﻿namespace ChinaUnicom.Converters
{
    using System;
    using System.Globalization;
    using System.Windows;
    using System.Windows.Data;

    public class DateConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var formatter = parameter == null ? "yyyy年MM月dd日" : parameter.ToString();

            switch (formatter)
            {
                case "YYYYMM":
                    formatter = "yyyy年MM月";
                    break;
            }

            DateTime date = (DateTime)value;
            return date.ToString(formatter);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string strValue = value as string;
            DateTime resultDateTime;
            if (DateTime.TryParse(strValue, out resultDateTime))
            {
                return resultDateTime;
            }

            return DependencyProperty.UnsetValue;
        }
    }

}
